import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;

public class AutoCompletePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField searchTextField = null;
	private JLabel label = null;
	private JList itemList = null;
	private DefaultListModel itemListModel = null;
	ArrayList<Item> items = new ArrayList<Item>();
	/**
	 * This is the default constructor
	 */
	public AutoCompletePanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2, 1));
		panel.add(getLabel());
		panel.add(getSearchTextField());
		this.add(panel, BorderLayout.NORTH);
		JScrollPane tmp = new JScrollPane(getItemList());
		tmp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		tmp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		this.add(tmp, BorderLayout.CENTER);
	}

	/**
	 * This method initializes searchTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getSearchTextField() {
		if (searchTextField == null) {
			searchTextField = new JTextField();
			searchTextField.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent e) {
					updateItemList();
				}
			});
		}
		return searchTextField;
	}
	
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel();
		}
		return label;
	}
	
	public void setLabel(String label) {
		getLabel().setText(label);
	}

	/**
	 * This method initializes itemList	
	 * 	
	 * @return javax.swing.JList	
	 */
	public JList getItemList() {
		if (itemList == null) {
			itemList = new JList(getItemListModel());
		}
		return itemList;
	}
	
	private DefaultListModel getItemListModel() {
		if (itemListModel == null) {
			itemListModel = new DefaultListModel();
		}
		return itemListModel;
	}
	
	public ArrayList<Item> getItems() {
		return items;
	}
	
	public Item getSelectedItem() {
		if (getItemList().getSelectedIndex() != -1) {
			String name =  (String) getItemListModel().elementAt(getItemList().getSelectedIndex());
			for (Item i : items) {
				if (i.name.equals(name)) return i;
			}
			return null;
		} else {
			return null;
		}
	}
	
	public ArrayList<Item> getSelectedItems() {
		ArrayList<Item> selectedItems = new ArrayList<Item>();
		int[] indices = getItemList().getSelectedIndices();
		for (int i = 0; i < indices.length; i++) {
			String name =  (String) getItemListModel().elementAt(indices[i]);
			for (Item item : this.items) {
				if (item.name.equals(name)) {
					selectedItems.add(item);
				}
			}
		}
		return selectedItems;
	}
	
	public void setItems(ArrayList<Item> items) {
		this.items = new ArrayList<Item>(items);
		updateItemList();
	}
	
	public void updateItemList() {
		DefaultListModel listModel = getItemListModel();
		if (searchTextField.getText().equals("")) {
			listModel.clear();
			for (Item item : items) {
				listModel.addElement(item.name);
			}
		} else {
			listModel.clear();
			for (Item item : items) {
				if (item.name.toLowerCase().contains(searchTextField.getText().toLowerCase())) {
					listModel.addElement(item.name);
				}
			}
		}
	}

}

class Item {
	int id = -1;
	String name = null;
	public Item() {
	}
	
	public Item(int id, String name) {
		this.id = id;
		this.name = name;
	}
}